function varargout = Call(hfile, method, varargin)
% BVQXfile::Call  - call a method (needed for older MATLAB versions)
%
% FORMAT:       [varargout] = Call(hObject, method, ...)
%
% Input fields:
%
%       hObject     1x1 BVQXfile object handle
%       method      name of method to call
%       ...         arguments
%
% Output fields:
%
%       varargout   as requested
%
% Note: this call usually produces the same effect as overloading
%       subsref, but in older releases this might be required to
%       obtain multiple outputs. So, 
%
% >> [output{1:nout}] = Call(hObject, 'method', ...);
%
%       and
%
% >> [output{1:nout}] = hObject.method(...);
%
%       should produce the same results.

% Version:  v0.7b
% Build:    7082921
% Date:     Aug-29 2007, 9:49 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global config
global bvqxfile_config;

% to check availability of functions use own dir!
persistent call_methdir;
if isempty(call_methdir)
    call_methdir = [fileparts(mfilename('fullpath')) '/private/'];
end

% argument check
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, true) || ...
   ~ischar(method) || ...
    isempty(method)
    error( ...
        'BVQXfile:BadSubsRef', ...
        'No S struct given or empty.' ...
    );
end
sc = bvqxfile_getscont(hfile.L);
otype = sc.S.Extensions{1};

% check all file types first
if exist([call_methdir 'aft_' method(:)' '.m'], 'file') == 2
    otype = 'aft';
end

% try call
try
    if nargout > 0
        varargout = cell(1, nargout);
        eval(['[varargout{1:nargout}]=' lower(otype) '_' method(:)' ...
              '(hfile, varargin{:});']);
    else

        eval(['[varargout{1}]=' lower(otype) '_' method(:)' ...
              '(hfile, varargin{:});']);
    end
catch
    error( ...
        'BVQXfile:CallError', ...
        'Error calling method ''%s'' of type ''%s''.', ...
        method(:)', ...
        upper(otype) ...
    );
end

% unwind stack
if bvqxfile_config.unwindstack
    BVQXfile(0, 'unwindstack');
end
